/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.alliance;

import cz.muni.fi.diplomka.servicelayer.dto.Alliance;
import cz.muni.fi.diplomka.servicelayer.dto.AllianceMember;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.AlliancesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.AlliancesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *
 * @author Jakub Kutil
 */
public class AllianceMemberPanel extends Panel {

    private AlliancesManager alliancesManager = new AlliancesManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();
     private CityManager cityManager = new CityManagerImpl();

    public AllianceMemberPanel(String id, PageParameters parameters) {
        super(id);

        if (parameters.containsKey("id")) {
            final Long playerId = parameters.getLong("id");

            Alliance alliance = alliancesManager.getAllianceOfPlayer(playerId);
            add(new Label("allianceInfo", "Alliance " + alliance.getName()));
            List<AllianceMember> list = new ArrayList<>();
            for (AllianceMember allianceMember : alliancesManager.getMembersOfAlliance(alliance.getId())) {
                list.add(allianceMember);
            }

            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    final AllianceMember allianceMember = (AllianceMember) item.getModelObject();
                    
                    item.add(new Label("name", playerManager.findPlayerById(allianceMember.getPlayerId()).getNickName()));
                    String cities = "";
                    for (City city: cityManager.getCitiesByPlayerId(allianceMember.getPlayerId())){
                        cities += city.getName() + " [" + city.getCoordinates().getX_coordinate() + "," + city.getCoordinates().getY_coordinate() +
                                "]" + "\n";
                    }
                    item.add(new MultiLineLabel("cities", cities));
                };
            };
            add(listview);

            Form<?> leaveAlliance = new Form<Void>("leaveAlliance") {
                @Override
                protected void onSubmit() {

                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", playerId.toString());
                    alliancesManager.leaveAlliance(playerId);
                    setResponsePage(AlliancePage.class, pageParameters);
                    
                }
            };

            add(leaveAlliance);
        }
    }
}